package com.dc.base.utils;

import java.security.MessageDigest;

public class MD5Util {
        private static String salt = "tc_project";

        public static String md5(String s) {
            try {
                s+=salt;
                //规定加密规则  MD5、DESC
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(s.getBytes("utf-8"));
                byte[] bytes = md.digest(s.getBytes("utf-8"));
                return toHex(bytes);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        private static String toHex(byte[] bytes) {
            //加密后的字符串组成
            final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
            StringBuilder ret = new StringBuilder(bytes.length * 2);
            for (int i = 0; i < bytes.length; i++) {
                ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
                ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
            }

            return ret.toString();
        }

        public static void main(String[] args) {

            System.out.println(md5("123456"));
        }

}
